package com.stx.jeepay.payment.channel.ysfpay.payway;

import com.alibaba.fastjson.JSONObject;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.payment.channel.ysfpay.YsfpayPaymentService;
import com.stx.jeepay.payment.model.MchAppConfigContext;
import com.stx.jeepay.payment.rqrs.AbstractRS;
import com.stx.jeepay.payment.rqrs.msg.ChannelRetMsg;
import com.stx.jeepay.payment.rqrs.payorder.UnifiedOrderRQ;
import com.stx.jeepay.payment.rqrs.payorder.payway.AliBarOrderRQ;
import com.stx.jeepay.payment.rqrs.payorder.payway.AliBarOrderRS;
import com.stx.jeepay.payment.utils.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 云闪付 支付宝 条码支付
 */
@Service("ysfPaymentByAliBarService")
public class AliBar extends YsfpayPaymentService {
    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrderEntity payOrder) {
        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        if (StringUtils.isEmpty(bizRQ.getAuthCode())) {
            throw new BizException("用户支付条码[authCode]不可为空");
        }

        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrderEntity payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        String logPrefix = "【云闪付条码(alipay)支付】";
        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        AliBarOrderRS res = ApiResBuilder.buildSuccess(AliBarOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);

        JSONObject reqParams = new JSONObject();
        reqParams.put("authCode", bizRQ.getAuthCode().trim()); // 付款码：用户展示的条码
        // 云闪付 bar 统一参数赋值
        barParamsSet(reqParams, payOrder);

        //客户端IP
        reqParams.put("termInfo", "{\"ip\": \"" + StringUtils.defaultIfEmpty(payOrder.getClientIp(), "127.0.0.1") + "\"}"); //终端信息

        // 发送请求
        JSONObject resJson = packageParamAndReq("/gateway/api/pay/micropay", reqParams, logPrefix, mchAppConfigContext);

        //请求 & 响应成功， 判断业务逻辑
        String respCode = resJson.getString("respCode");// 响应码
        String respMsg = resJson.getString("respMsg");//应答信息
        try {
            //00-交易成功， 02-用户支付中 , 12-交易重复， 需要发起查询处理    其他认为失败
            if ("00".equals(respCode)) {
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                res.setPayData(resJson.getString("payData"));
            } else if ("00".equals(respCode) || "12".equals(respCode) || "99".equals(respCode)) {
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                channelRetMsg.setNeedQuery(true); // 开启轮询查单
            } else {
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                channelRetMsg.setChannelErrCode(respCode);
                channelRetMsg.setChannelErrMsg(respMsg);
            }
        } catch (Exception e) {
            channelRetMsg.setChannelErrCode(respCode);
            channelRetMsg.setChannelErrMsg(respMsg);
        }
        return res;
    }
}
